/*     */ package com.zimbra.cs.localconfig;
/*     */ 
/*     */ import com.zimbra.common.localconfig.ConfigException;
/*     */ import com.zimbra.common.localconfig.ConfigWriter;
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.localconfig.LC.Reloadable;
/*     */ import com.zimbra.common.localconfig.LC.Supported;
/*     */ import com.zimbra.common.localconfig.LocalConfig;
/*     */ import com.zimbra.common.localconfig.Logging;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.SoapHttpTransport;
/*     */ import com.zimbra.common.util.CliUtil;
/*     */ import com.zimbra.common.util.RandomPassword;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.common.zclient.ZClientException;
/*     */ import com.zimbra.cs.account.soap.SoapProvisioning;
/*     */ import com.zimbra.soap.JaxbUtil;
/*     */ import com.zimbra.soap.admin.message.ReloadLocalConfigRequest;
/*     */ import java.io.IOException;
/*     */ import java.io.PrintStream;
/*     */ import java.lang.reflect.Field;
/*     */ import java.net.ConnectException;
/*     */ import java.util.Collection;
/*     */ import org.apache.commons.cli.CommandLine;
/*     */ import org.apache.commons.cli.CommandLineParser;
/*     */ import org.apache.commons.cli.GnuParser;
/*     */ import org.apache.commons.cli.HelpFormatter;
/*     */ import org.apache.commons.cli.Option;
/*     */ import org.apache.commons.cli.Options;
/*     */ import org.apache.commons.cli.ParseException;
/*     */ import org.dom4j.DocumentException;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class LocalConfigCLI
/*     */ {
/*  58 */   private final Options mOptions = new Options();
/*     */   
/*     */   private LocalConfigCLI() {
/*  61 */     this.mOptions.addOption("c", "config", true, "File in which configuration is stored.");
/*     */     
/*  63 */     this.mOptions.addOption("p", "path", false, "Show which configuration file will be used.");
/*     */     
/*  65 */     this.mOptions.addOption("e", "edit", false, "Edit configuration file, changing keys and values specified. [args] is in key=value form.");
/*     */     
/*  67 */     this.mOptions.addOption("r", "random", false, "Used with the edit option, sets specified key to random password string");
/*     */     
/*  69 */     this.mOptions.addOption("d", "default", false, "Show default values for keys listed in [args].");
/*     */     
/*  71 */     this.mOptions.addOption("n", "changed", false, "Show values for only those keys listed in [args] that have been changed from their defaults.");
/*     */     
/*  73 */     this.mOptions.addOption("i", "info", false, "Show documentation for keys listed in [args].");
/*     */     
/*  75 */     this.mOptions.addOption(null, "all", false, "Shows documentation for all keys, including unsupported ones");
/*     */     
/*  77 */     this.mOptions.addOption("x", "expand", false, "Expand values.");
/*     */     
/*  79 */     this.mOptions.addOption("s", "show", false, "Force display of password strings.");
/*     */     
/*  81 */     this.mOptions.addOption("f", "force", false, "Allow editing of keys whose change is known to be potentially dangerous.");
/*     */     
/*  83 */     this.mOptions.addOption("m", "format", true, "Show values in one of these formats: plain (default), xml, shell, export, nokey.");
/*     */     
/*  85 */     this.mOptions.addOption("q", "quiet", false, "Suppress logging.");
/*     */     
/*  87 */     this.mOptions.addOption("u", "unset", false, "Remove a configuration key.  If this is a key with compiled in defaults, set its value to the empty string.");
/*     */     
/*  89 */     this.mOptions.addOption("l", "reload", false, "Send a SOAP request to the server to reload its local config.");
/*     */     
/*  91 */     this.mOptions.addOption("h", "help", false, "Show this usage information.");
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private void usage()
/*     */   {
/* 101 */     Collection<Object> options = this.mOptions.getOptions();
/* 102 */     Options displayOptions = new Options();
/* 103 */     for (Object obj : options)
/*     */     {
/* 105 */       Option opt = (Option)obj;
/* 106 */       if (!"all".equals(opt.getLongOpt()))
/*     */       {
/* 108 */         displayOptions.addOption(opt);
/*     */       }
/*     */     }
/* 111 */     HelpFormatter formatter = new HelpFormatter();
/* 112 */     formatter.printHelp("zmlocalconfig [options] [args]", "where [options] are:", displayOptions, "");
/*     */     
/* 114 */     System.exit(0);
/*     */   }
/*     */   
/*     */   private void checkNotRoot(String option) {
/* 118 */     String username = System.getProperty("user.name");
/* 119 */     if ("root".equalsIgnoreCase(username)) {
/* 120 */       error("cannot use " + option + " when running as root", null);
/*     */     }
/*     */   }
/*     */   
/*     */   private void error(String errmsg, Exception e) {
/* 125 */     Logging.error(errmsg, e);
/* 126 */     System.exit(1);
/*     */   }
/*     */   
/*     */   private void exec(String[] args) {
/* 130 */     CommandLine cl = null;
/* 131 */     CommandLineParser parser = new GnuParser();
/*     */     try
/*     */     {
/* 134 */       cl = parser.parse(this.mOptions, args);
/*     */     } catch (ParseException pe) {
/* 136 */       Logging.error("Failed to parse command line: " + pe);
/* 137 */       System.exit(1);
/*     */     }
/*     */     
/* 140 */     if (cl.hasOption("q")) {
/* 141 */       Logging.setQuietMode(true);
/*     */     }
/*     */     
/* 144 */     if (cl.hasOption("h")) {
/* 145 */       usage();
/*     */     }
/*     */     
/*     */ 
/* 149 */     loadExtensionLC("com.zimbra.cs.backup.BackupLC");
/*     */     
/* 151 */     loadExtensionLC("com.zimbra.openoffice.config.OpenOfficeLC");
/*     */     
/* 153 */     loadExtensionLC("com.zimbra.cs.voice.VoiceLC");
/*     */     
/*     */ 
/* 156 */     if (cl.hasOption("i")) {
/* 157 */       checkCompatibleOptions("i", "q", cl);
/* 158 */       LocalConfig.printDoc(System.out, cl.getArgs(), false);
/* 159 */       return;
/*     */     }
/*     */     
/* 162 */     if (cl.hasOption("all")) {
/* 163 */       checkCompatibleOptions("all", "q", cl);
/* 164 */       LocalConfig.printDoc(System.out, cl.getArgs(), true);
/* 165 */       return;
/*     */     }
/*     */     
/* 168 */     LocalConfig lc = null;
/*     */     try {
/* 170 */       lc = new LocalConfig(cl.getOptionValue("c"));
/*     */     } catch (DocumentException de) {
/* 172 */       error("failed when reading config file", de);
/*     */     } catch (ConfigException ce) {
/* 174 */       error("failed with error in config file", ce);
/*     */     }
/*     */     
/*     */ 
/* 178 */     if (cl.hasOption("e")) {
/* 179 */       checkNotRoot("-e");
/* 180 */       checkCompatibleOptions("e", "qfrc", cl);
/* 181 */       String[] av = cl.getArgs();
/* 182 */       if ((av == null) || (av.length == 0)) {
/* 183 */         error("insufficient arguments", null);
/*     */       }
/* 185 */       for (int i = 0; i < av.length; i++) {
/* 186 */         String key = null;
/* 187 */         String value = null;
/* 188 */         if (cl.hasOption("r")) {
/* 189 */           key = av[i];
/* 190 */           value = RandomPassword.generate();
/*     */         } else {
/* 192 */           int eqidx = av[i].indexOf("=");
/* 193 */           if (eqidx <= 0)
/*     */           {
/* 195 */             error("argument '" + av[i] + "' not in key=value form", null);
/*     */           }
/* 197 */           key = av[i].substring(0, eqidx);
/* 198 */           value = av[i].substring(eqidx + 1, av[i].length());
/*     */         }
/* 200 */         if ((KnownKey.needForceToEdit(key)) && (!cl.hasOption("f"))) {
/* 201 */           error("can not edit key " + key, null);
/*     */         }
/* 203 */         lc.set(key, value);
/*     */       }
/*     */       try {
/* 206 */         lc.save();
/*     */       } catch (Exception e) {
/* 208 */         error("save to " + lc.getConfigFile() + " failed", e);
/*     */       }
/* 210 */       return;
/*     */     }
/*     */     
/*     */ 
/* 214 */     if (cl.hasOption("u")) {
/* 215 */       checkNotRoot("-u");
/* 216 */       checkCompatibleOptions("u", "qfc", cl);
/* 217 */       String[] av = cl.getArgs();
/* 218 */       if ((av == null) || (av.length == 0)) {
/* 219 */         error("insufficient arguments", null);
/*     */       }
/* 221 */       for (int i = 0; i < av.length; i++) {
/* 222 */         String key = av[i];
/* 223 */         if (!lc.isSet(key)) {
/* 224 */           error("key " + key + " is not set", null);
/*     */         }
/* 226 */         lc.remove(key);
/*     */       }
/*     */       try {
/* 229 */         lc.save();
/*     */       } catch (Exception e) {
/* 231 */         error("save to " + lc.getConfigFile() + " failed", e);
/*     */       }
/* 233 */       return;
/*     */     }
/*     */     
/*     */ 
/* 237 */     if (cl.hasOption("p")) {
/* 238 */       checkCompatibleOptions("p", "qc", cl);
/* 239 */       System.out.println(lc.getConfigFile());
/* 240 */       return;
/*     */     }
/*     */     
/* 243 */     if (cl.hasOption("l"))
/*     */     {
/* 245 */       CliUtil.toolSetup("WARN");
/*     */       try {
/* 247 */         reload();
/*     */       } catch (ServiceException e) {
/* 249 */         if ((e.getCause() instanceof ConnectException)) {
/* 250 */           error("'mailbox' service is not running", null);
/*     */         } else {
/* 252 */           error(e.getMessage(), e);
/*     */         }
/*     */       }
/* 255 */       return;
/*     */     }
/*     */     
/*     */ 
/* 259 */     String format = cl.getOptionValue("m");
/* 260 */     ConfigWriter cwriter = null;
/*     */     try {
/* 262 */       cwriter = ConfigWriter.getInstance(format, cl.hasOption("x"), !cl.hasOption("s"));
/*     */     } catch (ConfigException iae) {
/* 264 */       error("failed to create writer " + format, iae);
/*     */     }
/*     */     
/*     */     try
/*     */     {
/* 269 */       if (cl.hasOption("n")) {
/* 270 */         checkCompatibleOptions("n", "qscmx", cl);
/* 271 */         lc.printChanged(System.out, cwriter, cl.getArgs());
/* 272 */         return;
/*     */       }
/*     */       
/*     */ 
/* 276 */       if (cl.hasOption("d")) {
/* 277 */         checkCompatibleOptions("d", "qscmx", cl);
/* 278 */         lc.printDefaults(System.out, cwriter, cl.getArgs());
/* 279 */         return;
/*     */       }
/*     */       
/*     */ 
/* 283 */       checkCompatibleOptions("", "qscmx", cl);
/* 284 */       lc.print(System.out, cwriter, cl.getArgs());
/*     */     } catch (Exception e) {
/* 286 */       error("exception occurred when printing", e);
/*     */     }
/*     */   }
/*     */   
/*     */   private void loadExtensionLC(String className) {
/*     */     try {
/* 292 */       Class<?> lcClass = Class.forName(className);
/* 293 */       Field[] fields = lcClass.getFields();
/* 294 */       for (Field field : fields) {
/*     */         try {
/* 296 */           if (field.getType() == KnownKey.class) {
/* 297 */             KnownKey key = (KnownKey)field.get(null);
/*     */             
/* 299 */             key.setKey(field.getName());
/*     */             
/* 301 */             if (field.isAnnotationPresent(LC.Supported.class))
/* 302 */               key.setSupported(true);
/* 303 */             if (field.isAnnotationPresent(LC.Reloadable.class)) {
/* 304 */               key.setReloadable(true);
/*     */             }
/*     */           }
/*     */         }
/*     */         catch (Throwable never) {}
/*     */       }
/*     */     }
/*     */     catch (ClassNotFoundException e) {}
/*     */   }
/*     */   
/*     */   private void checkCompatibleOptions(String mainOption, String compatibleOptions, CommandLine cl)
/*     */   {
/* 316 */     Option[] opts = cl.getOptions();
/* 317 */     for (int i = 0; i < opts.length; i++) {
/* 318 */       String clOption = opts[i].getOpt() == null ? opts[i].getLongOpt() : opts[i].getOpt();
/* 319 */       if ((!mainOption.equals(clOption)) && (compatibleOptions.indexOf(clOption) == -1)) {
/* 320 */         if (mainOption.equals("")) {
/* 321 */           error("invalid option '" + clOption + "'", null);
/*     */         } else {
/* 323 */           error("option '" + clOption + "' can not be used with option '" + mainOption + "'", null);
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private void reload() throws ServiceException {
/* 330 */     String host = LC.zimbra_zmprov_default_soap_server.value();
/* 331 */     int port = LC.zimbra_admin_service_port.intValue();
/* 332 */     SoapHttpTransport transport = new SoapHttpTransport("https://" + host + ":" + port + "/service/admin/soap/");
/*     */     
/*     */ 
/* 335 */     SoapProvisioning prov = new SoapProvisioning();
/* 336 */     prov.soapSetURI(transport.getURI());
/* 337 */     prov.soapZimbraAdminAuthenticate();
/* 338 */     transport.setAuthToken(prov.getAuthToken());
/*     */     try
/*     */     {
/* 341 */       transport.invoke(JaxbUtil.jaxbToElement(new ReloadLocalConfigRequest()));
/*     */     } catch (IOException e) {
/* 343 */       throw ZClientException.IO_ERROR(e.getMessage(), e);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   public static void main(String[] args)
/*     */   {
/* 350 */     Logging.setUseZimbraLog(false);
/*     */     
/*     */ 
/* 353 */     ZimbraLog.toolSetupLog4jConsole("WARN", true, false);
/* 354 */     new LocalConfigCLI().exec(args);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/localconfig/LocalConfigCLI.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */